/*
 *
 * Created on: 02.11.2012
 *
 * DHMessageAnalyzer.cpp
 * This file is part of KOBUS
 * 
 * Copyright (C) 2012 - Ivan Penkin, MIPT
 * grek.penkin@gmail.com
 *
 * KOBUS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * KOBUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KOBUS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *  
 */


#include <DHMessageAnalyzer.h>


using namespace kobus;
using namespace dhcp;


DHMessageAnalyzer::ParsingParams::ParsingParams()
    :ifaceGrpNum(-1),
     vlanGrpNum(-1),
     macGrpNum(-1),
     ipGrpNum(-1),
     remoteIdGrpNum(-1),
     circuitIdGrpNum(-1){

};


DHMessageAnalyzer::Bufers::Bufers() {

  message.reserve(1024);
  for (int i = 0; i < MAX_ELEMS; i++) {
    hostInfo[i].reserve(64);
  };
}


void DHMessageAnalyzer::Bufers::clearInfo() {

  for (int i = 0; i < MAX_ELEMS; i++) {
      hostInfo[i].clear();
  };
}


DHMessageAnalyzer::DHMessageAnalyzer(QObject * parent)
    :QObject(parent) {

}


void DHMessageAnalyzer::setRegex(const QString & regex) {

  parsingParms.regex.setPattern(regex);
}

void DHMessageAnalyzer::setIfaceGrpNum(qint32 num) {

  parsingParms.ifaceGrpNum = num;
}
void DHMessageAnalyzer::setVlanGrpNum(qint32 num) {

  parsingParms.vlanGrpNum = num;
}

void DHMessageAnalyzer::setMacGrpNum(qint32 num) {

  parsingParms.macGrpNum = num;
}

void DHMessageAnalyzer::setIpGrpNum(qint32 num) {

  parsingParms.ipGrpNum = num;
}

void DHMessageAnalyzer::setRemoteIdGrpNum(qint32 num) {

  parsingParms.remoteIdGrpNum = num;
}

void DHMessageAnalyzer::setCircuitIdGrpNum(qint32 num) {

  parsingParms.circuitIdGrpNum = num;
}


void DHMessageAnalyzer::setMessage(const QString & message) {

  bufers.clearInfo();
  bufers.message = message;
}

#define CAP_GROUP(Index,GrpNum) \
    if (GrpNum > 0) { \
      bufers.hostInfo[Index] = \
        parsingParms.regex.cap(GrpNum); \
    }


void DHMessageAnalyzer::parse() throw (Exception) {

  Log log;
  log.debug(Log::FACILE, "DHMessageAnalyzer::parse() " + bufers.message);

  if (bufers.message.isEmpty()) {
    throw Exception(Exception::NO_ERRNO, "Message is empty");
  }

  if (parsingParms.regex.indexIn(bufers.message) > -1) {

    CAP_GROUP(Bufers::IFACE, parsingParms.ifaceGrpNum);
    CAP_GROUP(Bufers::VLAN, parsingParms.vlanGrpNum);
    CAP_GROUP(Bufers::MAC, parsingParms.macGrpNum);
    CAP_GROUP(Bufers::IP, parsingParms.ipGrpNum);
    CAP_GROUP(Bufers::REMOTE_ID, parsingParms.remoteIdGrpNum);
    CAP_GROUP(Bufers::CIRCUIT_ID, parsingParms.circuitIdGrpNum);
  } else {
    throw Exception(Exception::NO_ERRNO,
          "Wrong message. Regexp doesn't match.");
  }
}


HostInfo DHMessageAnalyzer::getNewHostInfoWithoutFlags()
                                                  throw (Exception) {

  check();

  HostInfo info(HostInfo::Types::DhcpInfo,
                  (quint32) 0,
                  (quint64) 0,
                  bufers.hostInfo[Bufers::IFACE],
                  bufers.hostInfo[Bufers::MAC],
                  bufers.hostInfo[Bufers::REMOTE_ID],
                  bufers.hostInfo[Bufers::CIRCUIT_ID],
                  bufers.hostInfo[Bufers::VLAN].toUInt(),
                  QHostAddress(bufers.hostInfo[Bufers::IP]),
                  this);

  return info;
}

